package com.lxm.shownews.channel;

import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.lxm.shownews.R;
import com.lxm.shownews.channel.fragment.AllChannelFragment;
import com.lxm.shownews.music.fragment.MusicFragment;
import com.lxm.shownews.utils.SPUtils;

public class DrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        initToolbar();
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, drawerToolbar, R.string.drawer_open, R.string.drawer_close);
        mDrawerToggle.syncState();
        drawerLayout.setDrawerListener(mDrawerToggle);
        ((NavigationView) findViewById(R.id.navigationView)).setNavigationItemSelectedListener(this);
        changeDrawer(R.id.navigation_item_channel);
    }

    private DrawerLayout drawerLayout;
    public Toolbar drawerToolbar;

    private void initToolbar() {
        drawerToolbar = (Toolbar) findViewById(R.id.drawer_toolbar);
        drawerToolbar.setTitle(R.string.drawer_menu_channel);
        setSupportActionBar(drawerToolbar);
//        drawerToolbar.setOnMenuItemClickListener(this);Toolbar.OnMenuItemClickListener,
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_drawer, menu);
//        return true;
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        int id = item.getItemId();
//
//        //noinspection SimplifiableIfStatement
//        if (id == R.id.action_settings) {
//            return true;
//        }
//
//        return super.onOptionsItemSelected(item);
//    }

//    @Override
//    public boolean onMenuItemClick(MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.navigation_item_channel:
//                changeDrawer(item.getItemId());
//                break;
//
//            case R.id.navigation_item_music:
//                changeDrawer(item.getItemId());
//                break;
//        }
//        return true;
//    }

    private static long currentMenuId;
    private static Fragment currentFragment;
    private AllChannelFragment allChannelFragment;
    private MusicFragment musicFragment;
    private Fragment[] fragments;

    private void changeDrawer(long id) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (id == R.id.navigation_item_channel) {
            if (allChannelFragment == null) {
                allChannelFragment = new AllChannelFragment();
//                ft.add(R.id.drawer_content, allChannelFragment);
            }
            currentFragment = allChannelFragment;
        }
        if (id == R.id.navigation_item_music) {
            if (musicFragment == null) {
                musicFragment = new MusicFragment();
//                ft.add(R.id.drawer_content, musicFragment);
            }
            currentFragment = musicFragment;

        }
        if (currentMenuId == id) {
            drawerLayout.closeDrawers();
            return;
        }
        ft.replace(R.id.drawer_content, currentFragment).commit();
        currentMenuId = id;
        drawerLayout.closeDrawers();
    }


    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_item_channel:
                changeDrawer(item.getItemId());
                item.setChecked(true);
                break;

            case R.id.navigation_item_music:
                changeDrawer(item.getItemId());
                item.setChecked(true);
                break;
        }
        return true;
    }
}
